import re

with open('C:\\Users\\zealo\Desktop\\人工智能比赛\\case2_data\\试题数据\\第二部分\\data.txt', 'r',encoding='UTF-8') as f:
    dictResult = {}

    # 每次读取一行的文件
    while True:
        line = f.readline()
        if not line:
            break
        listMatch = re.findall('\w+', line.lower())  # 转为小写字母
        # 计数
        for eachLetter in listMatch:
            eachLetterCount = len(re.findall(eachLetter, line.lower()))
            dictResult[eachLetter] = dictResult.get(eachLetter, 0) + eachLetterCount

    # 从大到小排序
    result = sorted(dictResult.items(), key=lambda d: d[1], reverse=True)[:10]
    for each in result:
        print(each)